<template>
  <div class="recommend-container">
    <div class="header-wrapper">
      <el-tabs v-model="activeTab" class="recommend-tabs">
        <el-tab-pane label="学习资料" name="interview" />
        <el-tab-pane label="招聘信息" name="recruitment" />
      </el-tabs>
      
      <el-dropdown trigger="click" class="filter-dropdown">
        <el-button type="primary" plain>
          <el-icon><Filter /></el-icon>
          <span>信息筛选</span>
        </el-button>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item 
              v-for="filter in filterOptions"
              :key="filter.value"
              @click="handleFilter(filter.value)"
            >
              {{ filter.label }}
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>

    <div class="content-wrapper">
      <el-scrollbar v-loading="loading">
        <div v-if="filteredNotifications.length === 0" class="empty-tips">
          暂无相关推荐信息
        </div>
        <div v-else class="notification-grid">
          <el-card 
            v-for="item in filteredNotifications" 
            :key="item.id"
            class="notification-card"
            shadow="hover"
            @click="handleDetailClick(item)"
          >
            
            <template #header>
              <div class="card-header">
                <div class="type-badge" :class="getTypeClass(item.notificationId)">
                  {{ getTypeName(item.notificationId) }}
                </div>
              </div>
            </template>
            
            <div class="card-content">
              <p class="content-text">{{ extractTitle(item.title) }}</p>
            </div>
            
          </el-card>
        </div>
      </el-scrollbar>
    </div>
    
    <TabNav />
  </div>
</template>

<script setup>
import { ref, computed, watch, onMounted } from 'vue'
import  { getPushNotifications }  from '@/views/recommend/api/index.js'
import { useRouter } from 'vue-router'
import TabNav from '@/components/TabNav/TabNav.vue'

const typeNameMap = {
  1: '面试技巧',
  2: '面试指导',
  3: '学习资料',
  4: '招聘信息'
}
// 新增类型类名映射方法
const getTypeClass = (type) => {
  const classMap = {
    1: 'interview-skills',
    2: 'interview-guidance',
    3: 'learning-materials',
    4: 'recruitment-info'
  }
  return classMap[type] || 'other'
}
const activeTab = ref('interview')
const selectedFilter = ref(null)
const notifications = ref([])
const loading = ref(false)
const router = useRouter()

const filterOptions = computed(() => {
  if (activeTab.value === 'interview') {
    return [
      { value: null, label: '全部' },
      { value: 1, label: '面试技巧' },
      { value: 2, label: '面试指导' },
      { value: 3, label: '学习资料' }
    ]
  }
  return []
})

const handleFilter = (value) => {
  selectedFilter.value = value
}

const filteredNotifications = computed(() => {
  let items = []
  
  // 根据标签页过滤基础数据
  if (activeTab.value === 'interview') {
    items = notifications.value.filter(item => [1, 2, 3].includes(item.notificationId))
  } else if (activeTab.value === 'recruitment') {
    items = notifications.value.filter(item => item.notificationId === 4)
  }
  // 应用筛选条件
  if (selectedFilter.value !== null) {
    items = items.filter(item => item.notificationId === selectedFilter.value)
  }
  
  return items
})

// 监听标签页变化重置筛选
watch(activeTab, () => {
  selectedFilter.value = null
})

const fetchNotifications = async () => {
  try {
    loading.value = true
    const res = await getPushNotifications()
    notifications.value = res.data.data
    console.log('数据获取成功:',res)
  } catch (error) {
    console.error('获取数据失败:', error)
  } finally {
    loading.value = false
  }
}

const handleDetailClick = (item) => {
  router.push({
    name: 'detail',
    params: { id: item.id }
    // params: { id: Number(item.id) }
  })
}

const getTypeName = (type) => {
  return typeNameMap[type] || '其他信息'
}
const extractTitle = (title) => {
  return title.replace(/【.*?】/, '')
}

onMounted(() => {
  fetchNotifications()
})
</script>

<style scoped>
.recommend-container {
  /* background: #d4e7ff;  */
  background: #ffffff;
  min-height: 100vh;
  margin: 0 auto;
}

.header-wrapper {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 12px;
  margin: 0 12px 12px 12px;
}
/* .content-wrapper {
  background: #ffffff;
  border-radius: 24px 24px 0 0;
  box-shadow: #409EFF 0 2px 8px;
  height: 80vh; 
  padding: 12px;
} */
.recommend-tabs {
  flex: 1;
}

.filter-dropdown {
  margin-left: 24px;
}
:deep(.el-card) {
  background: none;
  box-shadow: none;
  border: none;
  border-bottom: 1px solid var(--el-card-border-color);;
}
:deep(.el-card__header) {
  padding: 10px 20px;
  border: none;
}
:deep(.el-card__body) {
  padding: 10px 20px;
}

.notification-card:hover {
  transform: translateY(-4px);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: #606266;
  font-size: 14px;
}

.time-stamp {
  font-size: 12px;
  color: #909399;
}

.type-badge.interview-skills {
  background: #409EFF20;
  color: #409EFF;
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 4px;
}

.type-badge.interview-guidance {
  background: #67C23A20;
  color: #67C23A;
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 4px;
}

.type-badge.learning-materials {
  background: #9C27B020;
  color: #9C27B0;
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 4px;
}

.type-badge.recruitment-info {
  background: #E6A23C20;
  color: #E6A23C;
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 4px;
}

.content-text {
  color: #606266;
  line-height: 1.6;
  margin: 0;
  font-size: 14px;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
/* 加载状态提示 */
.loading-wrapper {
  display: flex;
  justify-content: center;
  padding: 40px 0;
}

/* 空数据提示 */
.empty-tips {
  text-align: center;
  color: #909399;
  padding: 40px 0;
}
</style>